package com.hollysys.usersync.controller;

import com.alibaba.fastjson2.JSON;
import com.hollysys.usersync.entity.*;
import com.hollysys.usersync.entity.platform.Organization;
import com.hollysys.usersync.service.SyncOrgService;
import com.hollysys.usersync.service.SyncPostService;
import com.hollysys.usersync.service.SyncUserService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户组织同步接口
 *
 * @author Jeremy Yim
 * @date 2023-04-03
 */
@RequestMapping(value = "/dataSync")
@RestController
@Log4j2
public class SyncController {

    @Resource
    private SyncUserService syncUserService;

    @Resource
    private SyncPostService syncPostService;

    @Resource
    private SyncOrgService syncOrgService;


    /**
     * 同步用户信息
     *
     * @return 同步结果
     */
    @PostMapping(value = "/user")
    private Response syncUsers(@RequestBody SyncUserRequest request, @RequestHeader(value = "token", defaultValue = "") String token) {

        log.info(request.getOption().getDescription() + "用户：" + request.getUserList().size());
        log.debug(request.getUserList());
        List<SyncResult> result;
        switch (request.getOption()) {
            case CREATE:
                result = syncUserService.addUsers(request.getUserList());
                break;
            case UPDATE:
                result = syncUserService.updateUsers(request.getUserList());
                break;
            case DELETE:
                result = syncUserService.deleteUsers(request.getUserList());
                break;
            default:
                return Response.builder().message("操作不存在。").status(999).timestamp(System.currentTimeMillis()).build();
        }
        log.info(JSON.toJSONString(request));
        log.info(JSON.toJSONString(result));
        return Response.builder().message("操作成功").status(0).timestamp(System.currentTimeMillis()).data(result).build();

    }

    /**
     * 同步岗位信息
     *
     * @return 同步结果
     */
    @PostMapping(value = "/post")
    private Response syncPost(@RequestBody SyncPostRequest request, @RequestHeader(value = "token", defaultValue = "") String token) {

        log.info(request.getOption().getDescription() + "岗位：" + request.getPostList().size());
        log.debug(request.getPostList());
        List<SyncResult> result;
        switch (request.getOption()) {
            case CREATE:
                result = syncPostService.addPost(request.getPostList());
                break;
            case UPDATE:
                result = syncPostService.updatePost(request.getPostList());
                break;
            case DELETE:
                result = syncPostService.deletePost(request.getPostList());
                break;
            default:
                return Response.builder().message("操作不存在。").status(999).timestamp(System.currentTimeMillis()).build();
        }
        log.info(JSON.toJSONString(request));
        log.info(JSON.toJSONString(result));
        return Response.builder().message("操作成功").status(0).timestamp(System.currentTimeMillis()).data(result).build();

    }

    /**
     * 同步组织信息
     *
     * @return 同步结果
     */
    @PostMapping(value = "/organization")
    private Response syncOrg(@RequestBody SyncOrgRequest request, @RequestHeader(value = "token", defaultValue = "") String token) {

        // TODO 这里转为北三定制逻辑，组织根节点编号MIS中为“1”，平台的为“platform_organization",在有效方案解决前，这里强制转换。
        List<Organization> organizationList = request.getOrgList().parallelStream().peek(org -> {
            if (Objects.equals(org.getParentCode(), "1")) {
                org.setParentCode("platform_organization");
            }
        }).collect(Collectors.toList());
        request.setOrgList(organizationList);

        log.info(request.getOption().getDescription() + "组织：" + request.getOrgList().size());
        log.debug(request.getOrgList());
        List<SyncResult> result;
        switch (request.getOption()) {
            case CREATE:
                result = syncOrgService.addOrganization(request.getOrgList());
                break;
            case UPDATE:
                result = syncOrgService.updateOrganization(request.getOrgList());
                break;
            case DELETE:
                result = syncOrgService.deleteOrganization(request.getOrgList());
                break;
            default:
                return Response.builder().message("操作不存在。").status(999).timestamp(System.currentTimeMillis()).build();
        }
        log.info(JSON.toJSONString(request));
        log.info(JSON.toJSONString(result));
        return Response.builder().message("操作成功").status(0).timestamp(System.currentTimeMillis()).data(result).build();

    }
}
